/*     */ package com.zimbra.cs.account.ldap.upgrade;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Entry.EntryType;
/*     */ import com.zimbra.cs.account.Provisioning.MailMode;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.ldap.LdapDIT;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.account.ldap.entry.LdapDomain;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZSearchScope;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class BUG_29978
/*     */   extends UpgradeOp
/*     */ {
/*     */   private static final int NUM_DOMAIN_SETS = 10;
/*     */   
/*     */   private static String genQuery(List<Server> servers)
/*     */   {
/*  43 */     StringBuffer q = new StringBuffer();
/*     */     
/*  45 */     int numValues = 0;
/*     */     
/*  47 */     for (Server server : servers) {
/*  48 */       String serviceName = server.getAttr("zimbraServiceHostname");
/*  49 */       if (!StringUtil.isNullOrEmpty(serviceName)) {
/*  50 */         q.append("(zimbraPublicServiceHostname=" + serviceName + ")");
/*  51 */         numValues++;
/*     */       }
/*     */     }
/*     */     
/*     */     String query;
/*     */     String query;
/*  57 */     if (numValues > 1) {
/*  58 */       query = "(|" + q.toString() + ")";
/*     */     } else {
/*  60 */       query = q.toString();
/*     */     }
/*     */     
/*  63 */     return "(&(objectClass=zimbraDomain)" + query + ")"; }
/*     */   
/*     */   private static class Bug29978Visitor extends SearchLdapOptions.SearchLdapVisitor { private UpgradeOp upgradeOp;
/*     */     private ZLdapContext modZlc;
/*     */     private Map<String, ServerInfo> serverMap;
/*     */     private int domainsVisited;
/*     */     
/*     */     class ServerInfo { String mServerName;
/*     */       String mProto;
/*     */       String mPort;
/*     */       
/*  74 */       ServerInfo(String serverName, String proto, String port) { this.mServerName = serverName;
/*  75 */         this.mProto = proto;
/*  76 */         this.mPort = port;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     Bug29978Visitor(UpgradeOp upgradeOp, ZLdapContext modZlc, List<Server> servers)
/*     */     {
/*  86 */       super();
/*  87 */       this.upgradeOp = upgradeOp;
/*  88 */       this.modZlc = modZlc;
/*     */       
/*  90 */       this.serverMap = new HashMap();
/*  91 */       for (Server server : servers)
/*     */       {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  97 */         String serviceName = server.getAttr("zimbraServiceHostname");
/*  98 */         if (!StringUtil.isNullOrEmpty(serviceName))
/*     */         {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 104 */           ServerInfo sInfo = (ServerInfo)this.serverMap.get(serviceName);
/* 105 */           if (sInfo != null) {
/* 106 */             upgradeOp.printer.println("Found duplicated zimbraServiceHostname, server honored:" + sInfo.mServerName + ", server ignored:" + server.getName());
/*     */           }
/*     */           else
/*     */           {
/* 110 */             String mailModeStr = server.getAttr("zimbraMailMode");
/* 111 */             Provisioning.MailMode mailMode = null;
/* 112 */             if (mailModeStr != null)
/*     */             {
/*     */ 
/*     */               try
/*     */               {
/* 117 */                 mailMode = Provisioning.MailMode.fromString(mailModeStr);
/*     */               }
/*     */               catch (ServiceException e) {}
/* 120 */               continue;
/*     */               
/*     */ 
/*     */ 
/* 124 */               String proto = null;
/* 125 */               String port = null;
/* 126 */               if (mailMode == Provisioning.MailMode.http) {
/* 127 */                 proto = "http";
/* 128 */                 port = server.getAttr("zimbraMailPort");
/* 129 */               } else if (mailMode == Provisioning.MailMode.https) {
/* 130 */                 proto = "https";
/* 131 */                 port = server.getAttr("zimbraMailSSLPort");
/*     */               }
/*     */               
/*     */ 
/*     */ 
/* 136 */               if (proto != null) {
/* 137 */                 this.serverMap.put(server.getAttr("zimbraServiceHostname"), new ServerInfo(server.getName(), proto, port));
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     public void visit(String dn, IAttributes ldapAttrs)
/*     */     {
/*     */       try {
/* 148 */         Domain domain = new LdapDomain(dn, (ZAttributes)ldapAttrs, this.upgradeOp.prov.getConfig().getDomainDefaults(), this.upgradeOp.prov);
/*     */         
/* 150 */         visit(domain);
/*     */       } catch (ServiceException e) {
/* 152 */         this.upgradeOp.printer.println("entry skipped, encountered error while processing entry at:" + dn);
/* 153 */         this.upgradeOp.printer.printStackTrace(e);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */     private void visit(Domain domain)
/*     */     {
/* 160 */       this.domainsVisited += 1;
/*     */       
/* 162 */       String domainPublicHostname = domain.getAttr("zimbraPublicServiceHostname");
/* 163 */       String domainPublicProtocol = domain.getAttr("zimbraPublicServiceProtocol");
/* 164 */       String domainPublicPort = domain.getAttr("zimbraPublicServicePort");
/*     */       
/*     */ 
/* 167 */       if (StringUtil.isNullOrEmpty(domainPublicHostname)) {
/* 168 */         if (this.upgradeOp.verbose) {
/* 169 */           this.upgradeOp.printer.format("Not updating domain %d: domain does not have %s\n", new Object[] { domain.getName(), "zimbraPublicServiceHostname" });
/*     */         }
/*     */         
/* 172 */         return;
/*     */       }
/*     */       
/*     */ 
/* 176 */       if (domainPublicProtocol != null) {
/* 177 */         if (this.upgradeOp.verbose) {
/* 178 */           this.upgradeOp.printer.format("Not updating domain %s: %s already set to %s on domain\n", new Object[] { domain.getName(), "zimbraPublicServiceProtocol", domainPublicProtocol });
/*     */         }
/*     */         
/* 181 */         return;
/*     */       }
/*     */       
/* 184 */       if (domainPublicPort != null) {
/* 185 */         if (this.upgradeOp.verbose) {
/* 186 */           this.upgradeOp.printer.format("Not updating domain %s: %s already set to %s on domain\n", new Object[] { domain.getName(), "zimbraPublicServicePort", domainPublicPort });
/*     */         }
/*     */         
/* 189 */         return;
/*     */       }
/*     */       
/*     */ 
/* 193 */       ServerInfo serverInfo = (ServerInfo)this.serverMap.get(domainPublicHostname);
/*     */       
/* 195 */       if (serverInfo == null) {
/* 196 */         if (this.upgradeOp.verbose) {
/* 197 */           this.upgradeOp.printer.format("Not updating domain %s\n", new Object[] { domain.getName() });
/*     */         }
/* 199 */         return;
/*     */       }
/*     */       
/* 202 */       Map<String, Object> attrs = new HashMap();
/*     */       
/* 204 */       if (serverInfo.mProto != null)
/* 205 */         attrs.put("zimbraPublicServiceProtocol", serverInfo.mProto);
/* 206 */       if ((serverInfo.mPort != null) && (!"0".equals(serverInfo.mPort))) {
/* 207 */         attrs.put("zimbraPublicServicePort", serverInfo.mPort);
/*     */       }
/*     */       try {
/* 210 */         this.upgradeOp.printer.format("Updating domain %-30s: proto => %-5s   port => %-5s\n", new Object[] { domain.getName(), attrs.get("zimbraPublicServiceProtocol"), attrs.get("zimbraPublicServicePort") });
/*     */         
/*     */ 
/*     */ 
/* 214 */         this.upgradeOp.modifyAttrs(this.modZlc, domain, attrs);
/*     */       }
/*     */       catch (ServiceException e) {
/* 217 */         this.upgradeOp.printer.println("Caught ServiceException while modifying domain " + domain.getName());
/* 218 */         this.upgradeOp.printer.printStackTrace(e);
/*     */       }
/*     */     }
/*     */     
/*     */     void reportStat() {
/* 223 */       this.upgradeOp.printer.println();
/* 224 */       this.upgradeOp.printer.println("Number of domains found = " + this.domainsVisited);
/* 225 */       this.upgradeOp.printer.println();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   void doUpgrade()
/*     */     throws ServiceException
/*     */   {
/* 237 */     List<Server> servers = this.prov.getAllServers();
/*     */     
/* 239 */     String query = genQuery(servers);
/*     */     
/* 241 */     String[] bases = this.prov.getDIT().getSearchBases(16);
/* 242 */     String[] attrs = { "objectClass", "zimbraId", "zimbraDomainName", "zimbraPublicServiceHostname", "zimbraPublicServiceProtocol", "zimbraPublicServicePort" };
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 249 */     ZLdapContext zlc = null;
/* 250 */     Bug29978Visitor visitor = new Bug29978Visitor(this, zlc, servers);
/*     */     try {
/* 252 */       zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
/*     */       
/* 254 */       for (String base : bases)
/*     */       {
/* 256 */         if (this.verbose) {
/* 257 */           this.printer.println("LDAP search base: " + base);
/* 258 */           this.printer.println("LDAP search query: " + query);
/* 259 */           this.printer.println();
/*     */         }
/*     */         
/* 262 */         SearchLdapOptions searchOpts = new SearchLdapOptions(base, getFilter(query), attrs, 0, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
/*     */         
/*     */ 
/*     */ 
/* 266 */         zlc.searchPaged(searchOpts);
/*     */       }
/*     */     } finally {
/* 269 */       LdapClient.closeContext(zlc);
/* 270 */       visitor.reportStat();
/*     */     }
/*     */   }
/*     */   
/*     */   Description getDescription()
/*     */   {
/* 276 */     return new Description(this, new String[] { "zimbraPublicServiceProtocol", "zimbraPublicServicePort" }, new Entry.EntryType[] { Entry.EntryType.DOMAIN }, null, String.format("value of %s and %s on the matching server", new Object[] { "zimbraPublicServiceProtocol", "zimbraPublicServicePort" }), "For each domain, if domain has zimbraPublicServiceHostname, and that zPSH has a corresponding zimbraServer, then set public service port/protocol on domain from that zimbraServer.");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private Server createTestServer(String serverName, String mailMode, String mailPort, String mailSSLPort)
/*     */     throws ServiceException
/*     */   {
/* 294 */     Map<String, Object> attrs = new HashMap();
/*     */     
/* 296 */     attrs.put("zimbraServiceHostname", serverName + "-serviceName");
/* 297 */     if (mailMode != null) {
/* 298 */       attrs.put("zimbraMailMode", mailMode);
/*     */     }
/* 300 */     if (mailPort != null) {
/* 301 */       attrs.put("zimbraMailPort", mailPort);
/*     */     }
/* 303 */     if (mailSSLPort != null) {
/* 304 */       attrs.put("zimbraMailSSLPort", mailSSLPort);
/*     */     }
/* 306 */     this.printer.println("Creating server " + serverName);
/* 307 */     return this.prov.createServer(serverName, attrs);
/*     */   }
/*     */   
/*     */   private Domain createTestDomain(String domainName, String publicServiceHostname) throws ServiceException
/*     */   {
/* 312 */     Map<String, Object> attrs = new HashMap();
/*     */     
/* 314 */     if (publicServiceHostname != null) {
/* 315 */       attrs.put("zimbraPublicServiceHostname", publicServiceHostname);
/*     */     }
/* 317 */     this.printer.println("Creating domain " + domainName);
/* 318 */     return this.prov.createDomain(domainName, attrs);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private Domain createTestDomain(String domainName, String publicServiceHostname, String publicServiceProtocol, String publicServicePort)
/*     */     throws ServiceException
/*     */   {
/* 329 */     Map<String, Object> attrs = new HashMap();
/*     */     
/* 331 */     if (publicServiceHostname != null)
/* 332 */       attrs.put("zimbraPublicServiceHostname", publicServiceHostname);
/* 333 */     if (publicServiceProtocol != null)
/* 334 */       attrs.put("zimbraPublicServiceProtocol", publicServiceProtocol);
/* 335 */     if (publicServicePort != null) {
/* 336 */       attrs.put("zimbraPublicServicePort", publicServicePort);
/*     */     }
/* 338 */     this.printer.println("Creating domain " + domainName);
/* 339 */     return this.prov.createDomain(domainName, attrs);
/*     */   }
/*     */   
/*     */ 
/*     */   private void populateTestData()
/*     */     throws ServiceException
/*     */   {
/* 346 */     for (Server s : this.prov.getAllServers()) {
/* 347 */       if (!s.getName().equals("phoebe.mac")) {
/*     */         try {
/* 349 */           this.prov.deleteServer(s.getId());
/* 350 */           this.printer.println("Deleted server " + s.getName());
/*     */         }
/*     */         catch (ServiceException e) {}
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 357 */     this.printer.println();
/*     */     
/* 359 */     Server http = createTestServer("http.server", Provisioning.MailMode.http.toString(), "1000", "1001");
/* 360 */     Server http_portis0 = createTestServer("http_portis0.server", Provisioning.MailMode.http.toString(), "0", "1001");
/* 361 */     Server http_noport = createTestServer("http_noport.server", Provisioning.MailMode.http.toString(), null, "1001");
/*     */     
/* 363 */     Server https = createTestServer("https.server", Provisioning.MailMode.https.toString(), "2000", "2001");
/* 364 */     Server https_portis0 = createTestServer("https_portis0.server", Provisioning.MailMode.https.toString(), "2000", "0");
/* 365 */     Server https_noport = createTestServer("https_noport.server", Provisioning.MailMode.https.toString(), "2000", null);
/*     */     
/* 367 */     Server mixed = createTestServer("mixed.server", Provisioning.MailMode.mixed.toString(), "3000", "3001");
/* 368 */     Server both = createTestServer("both.server", Provisioning.MailMode.both.toString(), "4000", "4001");
/* 369 */     Server redirect = createTestServer("redirect.server", Provisioning.MailMode.redirect.toString(), "5000", "5001");
/*     */     
/* 371 */     Server nomailmode = createTestServer("nomailmode.server", null, "7000", "7001");
/*     */     
/* 373 */     for (int i = 0; i < 10; i++) {
/* 374 */       createTestDomain("http_" + i + ".test", http.getAttr("zimbraServiceHostname"));
/* 375 */       createTestDomain("http_portis0_" + i + ".test", http_portis0.getAttr("zimbraServiceHostname"));
/* 376 */       createTestDomain("http_noport_" + i + ".test", http_noport.getAttr("zimbraServiceHostname"));
/*     */       
/* 378 */       createTestDomain("https_" + i + ".test", https.getAttr("zimbraServiceHostname"));
/* 379 */       createTestDomain("https_portis0_" + i + ".test", https_portis0.getAttr("zimbraServiceHostname"));
/* 380 */       createTestDomain("https_noport_" + i + ".test", https_noport.getAttr("zimbraServiceHostname"));
/*     */       
/* 382 */       createTestDomain("mixed_" + i + ".test", mixed.getAttr("zimbraServiceHostname"));
/* 383 */       createTestDomain("both_" + i + ".test", both.getAttr("zimbraServiceHostname"));
/* 384 */       createTestDomain("redirect_" + i + ".test", redirect.getAttr("zimbraServiceHostname"));
/*     */       
/* 386 */       createTestDomain("nomailmode_" + i + ".test", nomailmode.getAttr("zimbraServiceHostname"));
/*     */       
/*     */ 
/* 389 */       createTestDomain("notmatch" + i + ".test", "notmatch");
/*     */       
/*     */ 
/* 392 */       createTestDomain("no_PSH" + i + ".test", null);
/*     */       
/*     */ 
/* 395 */       createTestDomain("proto_present_" + i + ".test", http.getAttr("zimbraServiceHostname"), "https", null);
/*     */       
/*     */ 
/* 398 */       createTestDomain("port_present_" + i + ".test", http.getAttr("zimbraServiceHostname"), null, "8888");
/*     */     }
/*     */     
/*     */ 
/* 402 */     this.printer.println("\ndone");
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 406 */     BUG_29978 op = (BUG_29978)LdapUpgrade.getUpgradeOpUnitTest("29978");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 411 */     op.doUpgrade();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/upgrade/BUG_29978.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */